﻿using Microsoft.AspNetCore.Mvc;

namespace 动态api
{
    public static class SimleDynamicWebApiExtensions
    {
        public static IMvcBuilder AddDynamicWebApi(this IMvcBuilder builder)
        {
            if (builder == null) throw new ArgumentNullException(nameof(builder));
            builder.ConfigureApplicationPartManager(applicationPartManager =>
            {
                //微软官方定义：
                //ApplicationPartManager 管理 MVC 应用程序的部件和功能。
                //注入我们自定义的实现类
                //到此一步实现了将所有实现了IApplicationService的类都视作控制器
                applicationPartManager.FeatureProviders.Add(new ApplicationServiceControllerFeatureProvider());
            });
            builder.Services.Configure<MvcOptions>(options =>
            {
                options.Conventions.Add(new ApplicationServiceConvention());
            });
            return builder;
        }

        public static IMvcCoreBuilder AddDynamicWebApi(this IMvcCoreBuilder builder)
        {
            if (builder == null) throw new ArgumentNullException(nameof(builder));

            builder.ConfigureApplicationPartManager(applicationPartManager =>
            {
                applicationPartManager.FeatureProviders.Add(new ApplicationServiceControllerFeatureProvider());
            });
            builder.Services.Configure<MvcOptions>(options =>
            {
                options.Conventions.Add(new ApplicationServiceConvention());
            });
            return builder;
        }
    }
}